import axios from 'axios'
//导入进度条组件
import  NProgress  from 'nprogress'

export function request(config) {
  //1.创建axios的实例
  const instance = axios.create({
    baseURL: 'http://node.w-bf.com/api/private/v1/',
    timeout: 5000
  })

  //2.axios的拦截器
  //2.1.请求拦截器的作用
  instance.interceptors.request.use(config => {
    //为请求头对象，添加token验证的 Authorization 字段
    config.headers.Authorization = window.sessionStorage.getItem('token')
    //展示进度条
    NProgress.start()
    return config
  },
  err => {
    //console.log(err)
  })

  //2.2.响应拦截
  instance.interceptors.response.use(res => {
    NProgress.done()
    return res.data
  },
  err => {
    console.log(err)
  })

  //3.发送真正的网络请求
  return instance(config)
}
